﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServerAdapter.Utils;
using System.ServiceModel;
using ServerAdapter.WcfGeebaService;

namespace ServerAdapter
{
    public class SAdapter
    {
        #region DataMemebers

        private WcfGeebaService.ServiceClient server;
        private string serverKey = EncDec.getServerKey();

        #endregion DataMemebers

        #region Cstor

        public SAdapter(ClientListener client)
        {
            // Web Service settings
            InstanceContext context = new InstanceContext(client);     // Sending IntanceContex to Server that it will be able to make callbacks
            server = new WcfGeebaService.ServiceClient(context);
        }

        public void Dispose()
        {
            server.unsubscribe();
            server.Close();
        }

        public void serverSubscribe(string username)
        {
            server.subscribe(username);
        }

        #endregion

        #region ServerMethods

        public bool serverInit(string admin,string adminPass,GPolicy policy)
        {
            adminPass = EncDec.Encrypt(adminPass, serverKey);
            return this.server.init(admin, adminPass, policy);
        }

        public User serverEntry()
        {
            return this.server.entry();
        }

        public bool serverRegister(string username, string password)
        {
            password = EncDec.Encrypt(password, serverKey);
            return this.server.register(username, password);
        }

        public Member serverLogin(User olduser, string username, string password)
        {
            password = EncDec.Encrypt(password, serverKey);
            return this.server.login(olduser, username, password);
        }

        public User serverLogout(User member)
        {
            if (member is Member)
            {
                return this.server.logout(member.username);
            }
            return null;
        }

        public List<GSubForum> serverGetSubForums()
        {
            return this.server.getSubForums().ToList<GSubForum>();
        }

        public List<GThread> serverGetThreads(GSubForum subForum)
        {
            return this.server.getThreads(subForum).ToList<GThread>();
        }

        public int serverPublish(Member requesting, string topic, GThread newMessage)
        {
            return server.publish(requesting,topic, newMessage);
        }

        // new methods of version 2:
        public bool serverAddSubForum(Member requesting,string topic, string moderator)
        {
            return server.addSubForum(requesting, topic, moderator);
        }

        public bool serverRemoveSubForum(Member requesting,string topic)
        {
            return server.removeSubForum(requesting, topic);
        }

        public int serverGetMemberThreadCount(Member requesting,string username)
        {
            return server.getMemberThreadCount(requesting,username);
        }

        public bool serverRemoveThread(Member requesting,string topic, int threadID)
        {
            return server.removeThread(requesting, topic, threadID);
        }

        public bool serverAddModerator(Member requesting, string topic, string memberUsername)
        {
            return server.addModerator(requesting, topic, memberUsername);
        }

        public bool serverReplaceModerator(Member requesting,string topic, string oldModerator, string newModerator)
        {
            return server.replaceModerator(requesting, topic, oldModerator, newModerator);
        }

        public bool serverRemoveModerator(Member requesting, string topic, string moderator)
        {
            return server.removeModerator(requesting, topic, moderator);
        }

        public int serverGetThreadCount(Member requesting,string topic)
        {
            return server.getThreadCount(requesting, topic);
        }

        // Gthread methods
        //public int comment(GPost message, GThread thread);

        public IEnumerable<GThread> getThreads(User requesting,GSubForum gSubForum)
        {
            return server.getThreads(gSubForum);
        }

        #endregion

        public bool serverEditContent(Member requesting, string topic, int thId, string newContent)
        {
            return server.editContent(requesting, topic, thId, newContent);
        }

        public bool serverBumpThread(Member requesting, string topic, int thId)
        {
            return server.bumpThread(requesting, topic, thId);
        }

        public int commentOnThread(Member requesting, GPost post, string topic, int thId)
        {
            return server.comment(requesting, post, topic, thId);
        }

        public GPost[] serverGetComments(User requesting, string topic, int threadId)
        {
            User user = new User();
            user.username = requesting.username;
            return server.getComments(topic, threadId);//.ToList<GPost>();
        }
        

        public bool changePassword(Member requesting, string pass, string newPass)
        {
            pass = EncDec.Encrypt(pass, serverKey);
            newPass = EncDec.Encrypt(newPass, serverKey);
            return server.changePassword(requesting, pass, newPass);
        }

        public int getOnlineUserCount()
        {
            return server.getConnectedMembersCount();
        }

        public GThread[] searchByContent(string content)
        {
            return server.searchByContent(content);
        }

        public GThread[] searchByUser(string username)
        {
            return server.searchByUser(username);
        }
    }
}
